package entity

import (
	"fmt"
	"time"

	"shqsoft.cn/pas3ui/base"

	"gorm.io/gorm"
)

// ConfSttp FTP曲线
type ConfPara struct {
	ID   int      `gorm:"primary_key;column:PANO"`
	Name string   `gorm:"column:PANM;size:50"`
	PATP int      `gorm:"column:PATP"`
	STNO int      `gorm:"column:STNO"`
	UNIT string   `gorm:"column:UNIT;size:50"`
	PAVL *float64 `gorm:"-"`
}

// TableName FTP曲线类型表名
func (ConfPara) TableName() string {
	return "T_CONF_PARA"
}

// BasePara FTP曲线
type BasePara struct {
	ID       int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	PAID     int        `gorm:"column:PANO"`
	ConfPara ConfPara   `gorm:"ForeignKey:PAID"`
	PAVL     float64    `gorm:"column:PAVL"`
	BGDT     *time.Time `gorm:"column:BGDT"`
	ENDT     *time.Time `gorm:"column:ENDT"`
	STNO     int        `gorm:"column:STNO"`
	PANM     string     `gorm:"-"`
}

// TableName FTP曲线表名
func (BasePara) TableName() string {
	return "T_BASE_PARA"
}

func (u *BasePara) BeforeSave(tx *gorm.DB) (err error) {
	var paid *int
	err = tx.Raw(`SELECT "PANO" FROM "T_CONF_PARA" WHERE "PANM" = ?`, u.PANM).Row().Scan(&paid)
	if err != nil || paid == nil {
		err = fmt.Errorf("该配置%s系统内不存在", u.PANM)
		return
	}
	u.PAID = *paid
	return
}

// 判断新增是否存在
func (p *BasePara) BeforeCreate(tx *gorm.DB) (err error) {
	rows, err := tx.Raw(`SELECT * FROM "T_BASE_PARA" WHERE "BGDT"=? AND "PANO"=?`, p.BGDT, p.PAID).Rows()
	if err != nil {
		return err
	}
	defer rows.Close()
	if rows.Next() {
		return fmt.Errorf("生效日期%s已存在，无法再次添加！", p.BGDT.Format("20060102"))
	}
	var count int
	err = tx.Raw(`SELECT count(*) FROM "T_BASE_PARA" WHERE "PANO"=?`, p.PAID).Row().Scan(&count)
	if err != nil {
		return err
	}
	if count > 0 {
		sqlStr := `UPDATE "T_BASE_PARA" SET "ENDT"=? WHERE "PANO"=? AND ("ENDT"='2099-12-31' OR "ENDT" IS NULL)`
		err = tx.Exec(sqlStr, p.BGDT.AddDate(0, 0, -1), p.PAID).Error
		if err != nil {
			return err
		}
	}
	err = tx.Raw(`select "STNO" from "T_CONF_PARA" where "PANO"=?`, p.PAID).Row().Scan(&p.STNO)
	if err != nil {
		return err
	}
	endt, _ := time.Parse("2006-01-02", "2099-12-31")
	p.ENDT = &endt
	return
}
func (p *BasePara) BeforeUpdate(tx *gorm.DB) (err error) {
	err = tx.Raw(`select "STNO" from "T_CONF_PARA" where "PANO"=?`, p.PAID).Row().Scan(&p.STNO)
	return err
}

// BaseParaEF 一个结构
func ConfParaEF() base.EntityFunc {
	return func() interface{} {
		entity := ConfPara{}
		return &entity
	}
}

// BaseParaESF 数组结构体
func ConfParaESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ConfPara
		return &entities
	}
}

// BaseParaEF 一个结构
func BaseParaEF() base.EntityFunc {
	return func() interface{} {
		entity := BasePara{}
		return &entity
	}
}

// BaseParaESF 数组结构体
func BaseParaESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []BasePara
		return &entities
	}
}

func init() {
	relationship := []string{"ConfPara"}
	base.DaoPoolObj.Put("BasePara", base.NewDefaultDao(BaseParaEF(), BaseParaESF(), relationship, "my"))
	base.DaoPoolObj.Put("ConfPara", base.NewDefaultDao(ConfParaEF(), ConfParaESF(), nil, "my"))
}
